//----------------Sonic CD Time Stone Script------------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Sonic Aliases
private alias Object.State : SSSonic.State
private alias Object.Frame : SSSonic.Frame

// States
private alias 0  : TIMESTONE_FALLING
private alias 1  : TIMESTONE_HELD

// Special Stage Sonic States
private alias 10 : SSSONIC_STONEGRABBED

// SFX Aliases
private alias 21 : SFX_G_ACHIEVEMENT


event ObjectDraw
	// Get the frame the Stone should draw
	// (But note that it's not drawn till the end, see there)
	temp0 = Object.Frame
	temp0 /= 12
	
	// Animate the Stone
	Object.Frame++
	if Object.Frame > 47
		Object.Frame = 0
	end if
	
	if Object.State == TIMESTONE_FALLING
		// Stone's still falling, so draw the sparkles that it's dropping
		
		// Smaller sparkle, to be 32 pixels above the Time Stone
		temp1 = Object.YPos
		temp1 -= 0x200000
		DrawSpriteFX(5, FX_FLIP, Object.XPos, temp1)
		
		// Bigger sparkle, 12 pixels below the smaller sparkle (absolute difference of 20 pixels above the Time Stone)
		temp1 += 0xC0000
		DrawSpriteFX(4, FX_FLIP, Object.XPos, temp1)
		
		// Flip the object when needed in order to make the rotating animation appear smoother
		Object.Direction = Object.Frame
		Object.Direction %= 24
		Object.Direction /= 6
		
		if Object.iYPos < 200
			// Time Stone isn't at its target destination yet, keep it falling at a rate of 1.5 px per frame
			Object.YPos += 0x18000
		else
			
			// Snap to Sonic's hands
			Object.iYPos = 200
			
			// Stop further movement of the Stone
			Object.State = TIMESTONE_HELD
			PlaySfx(SFX_G_ACHIEVEMENT, false)
			
			// Signal to the Sonic Object that he's grabbed the Stone, from here he'll handle the rest
			SSSonic[2].State = SSSONIC_STONEGRABBED
			SSSonic[2].Frame++
			
#platform: Use_Origins
			// Let the Engine know we've touched an... "emerald"?
			EngineCallback(NOTIFY_TOUCH_EMERALD)
#endplatform
			
		end if
	end if
	
	// Draw the actual Stone sprite
	// It's drawn all the way at the end of the process so that it stays above the sparkles, drawn before
	DrawSprite(temp0)
	
end event


event ObjectStartup
	
	LoadSpriteSheet("3D_Test/Objects.gif")
	
	// 0-3 - Time Stone Frames
	SpriteFrame(-10, -12, 20, 24, 1, 232)
	SpriteFrame(-10, -12, 20, 24, 22, 232)
	SpriteFrame(-10, -12, 20, 24, 43, 232)
	SpriteFrame(-10, -12, 20, 24, 64, 232)
	
	// 4-5 - Sparkles
	SpriteFrame(-12, -12, 24, 24, 83, 143)
	SpriteFrame(-8, -8, 16, 16, 83, 126)
	
	// No extra sprites needed for alternate colors - they're handled via palettes stored in the StageConfig instead
	
end event


// ========================
// Editor events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("3D_Test/Objects.gif")
	SpriteFrame(-10, -12, 20, 24, 1, 232)

	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
